package com.hospital.phyexam.impl;

import com.alibaba.fastjson.JSON;
import com.hospital.phyexam.dao.*;
import com.hospital.phyexam.entity.Phyexam;
import com.hospital.phyexam.entity.PhyexamBatch;
import com.hospital.phyexam.entity.RelationPhyBatchKey;
import com.hospital.phyexam.entity.Student;
import com.hospital.phyexam.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: Hospital
 * @description: 学生
 * @author: 杨曼珍
 * @create: 2019-01-12 19:05
 **/
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    StudentMapper studentMapper;
    @Autowired
    PhyexamProjectMapper projectMapper;
    @Autowired
    PhyexamMapper phyexamMapper;
    @Autowired
    PhyexamBatchMapper batchMapper;
    @Autowired
    RelationPhyBatchMapper relationMapper;

    public List getAll(Phyexam phyexam){
        List<Map<String,String>>resaultmap=new ArrayList<>();
       PhyexamBatch phyexamBatch=batchMapper.selectByPrimaryKey(phyexam.getPhyExmBatchId());
       Student student=studentMapper.selectById(phyexam.getPhyExmStuId());
       List<RelationPhyBatchKey>relationPhyBatchKeys=relationMapper.selectByBatchId(phyexamBatch.getId());
       for (RelationPhyBatchKey relationPhyBatchKey:relationPhyBatchKeys){
           Map<String,String>map=new HashMap<>();
           map.put("proName",projectMapper.selectByPrimaryKey(relationPhyBatchKey.getRltPhyProId()).getProName());
           map.put("phyExmBatchName",phyexamBatch.getPhyExmBatchName());
           map.put("phyExmBatchBeginTime",new SimpleDateFormat("yyyy-MM-dd").format(phyexamBatch.getPhyExmBatchBeginTime()));
           map.put("phyExmBatchOverTime",new SimpleDateFormat("yyyy-MM-dd").format(phyexamBatch.getPhyExmBatchOverTime()));
           map.put("stuName",student.getStuName());
           map.put("id",phyexam.getId().toString());
           resaultmap.add(map);
       }
     return resaultmap;
    }

    @Override
    public String getAllStu(){
        List<Map<String,String>>resaultmap=new ArrayList<>();
        List<Phyexam>phyexams=phyexamMapper.selectAll();
        for (Phyexam phyexam:phyexams){
            resaultmap.addAll(getAll(phyexam));
        }
        return JSON.toJSONString(resaultmap);
    }

    @Override
    public String selectByStuName(String name){
        List<Map<String,String>>resaultmap=new ArrayList<>();
        List<Student>students=studentMapper.selectByName(name);
        for (Student student:students){
            List<Phyexam>phyexams=phyexamMapper.selectByStuId(student.getId());
            for (Phyexam phyexam:phyexams){
                resaultmap.addAll(getAll(phyexam));
            }
        }
        return JSON.toJSONString(resaultmap);
    }

    @Override
    public String selectByBatchName(String name){
        List<Map<String,String>>resaultmap=new ArrayList<>();
       PhyexamBatch phyexamBatch=batchMapper.selectByBatchName(name);
            List<Phyexam>phyexams=phyexamMapper.selectByBatchId(phyexamBatch.getId());
            for (Phyexam phyexam:phyexams){
                resaultmap.addAll(getAll(phyexam));
                System.out.println(resaultmap);

        }
        return JSON.toJSONString(resaultmap);
    }
    @Override
    public String add(int batchId,int deptId){
        List<Student>students=studentMapper.selectByDepartId(deptId);
        for (Student student:students){
            Phyexam phyexam=new Phyexam();
            phyexam.setPhyExmStuId(student.getId());
            phyexam.setPhyExmBatchId(batchId);
            phyexamMapper.insert(phyexam);
        }
        return getAllStu();
    }

/*    @Override
    public String delete(int batchId,int deptId){
        List<Student>students=studentMapper.selectByDepartId(deptId);
        for (Student student:students){
            Phyexam phyexam=new Phyexam();
            phyexam.setPhyExmStuId(student.getId());
            phyexam.setPhyExmBatchId(batchId);
            phyexamMapper.insert(phyexam);
        }
        return getAllStu();
    }*/



}
